package me.gacl.demo;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

import org.junit.Test;

import me.gacl.utils.JdbcUtils;



/**
* @ClassName: JdbcBatchHandleByStatement
* @Description: 使用prepareStatement实现JDBC批处理操作
* @author: 孤傲苍狼
* @date: 2014-9-20 下午10:05:45
*
*/ 

public class JdbcBatchHandleByPrepareStatement extends JdbcBatchHandleByStatement {

	@Test
	public void testJdbcBatchHandleByPrepareStatement() {
		long starttime = System.currentTimeMillis();
		Connection conn = null;
		PreparedStatement st = null;
		ResultSet rs = null;
		try {
			conn = JdbcUtils.getConnection();
			String sql = "insert into testbatch(id,name) values(?,?)";
			st = conn.prepareStatement(sql);
			for(int i =1; i < 10008;i++) {//i=1000  2000
				st.setInt(1, i);
				st.setString(2, "aa"+i);
				st.addBatch();
				if(i%1000 == 0) {
					st.executeBatch();
					st.clearBatch();
				}
			}
			st.executeBatch();
			st.clearBatch();
		}catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}finally {
			JdbcUtils.release(conn, st, rs);
		}
		long endtime = System.currentTimeMillis();
        System.out.println("程序花费时间：" + (endtime-starttime)/1000 + "秒！！");
	}
}
